﻿// <copyright file="XmlNodeReader.cs" company="Signal Processing Lab, Wuhan University">
// Copyright (c) 2009-2010 All Right Reserved
// </copyright>
// <author>Xin-ping Deng</author>
// <email>dentoby@gmail.com</email>
// <date>2010-4</date>
// <summary>This is part of Image.NET project.</summary>

namespace SPLWHU.Core
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Xml;

    /// <summary>
    /// The xml node reader.
    /// </summary>
    public class XmlNodeReader
    {
        /// <summary>
        /// The attached xml node.
        /// </summary>
        private XmlElement node;

        /// <summary>
        /// Initializes a new instance of the <see cref="XmlNodeReader"/> class.
        /// </summary>
        /// <param name="filename">The filename.</param>
        public XmlNodeReader(string filename)
        {
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(filename);
            this.node = (XmlElement)xmlDoc.FirstChild;
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="XmlNodeReader"/> class.
        /// </summary>
        /// <param name="node">The xml node.</param>
        private XmlNodeReader(XmlElement node)
        {
            this.node = node;
        }

        /// <summary>
        /// Gets the child count.
        /// </summary>
        /// <value>The child count.</value>
        public int ChildCount
        {
            get { return this.node.ChildNodes.Count; }
        }

        /// <summary>
        /// Gets the attribute count.
        /// </summary>
        /// <value>The attribute count.</value>
        public int AttributeCount
        {
            get { return this.node.Attributes.Count; }
        }

        /// <summary>
        /// Gets the name of the xml node.
        /// </summary>
        /// <value>The name of the xml node.</value>
        public string Name
        {
            get { return this.node.Name; }
        }

        /// <summary>
        /// Gets the value of this node.
        /// </summary>
        /// <value>The value of this node.</value>
        public string Value
        {
            get { return this.node.Value; }
        }

        /// <summary>
        /// Gets the specified attribute.
        /// </summary>
        /// <param name="idx">The index of the attribute.</param>
        /// <param name="name">The name of the attribute.</param>
        /// <param name="value">The value of the attribute.</param>
        public void GetAttribute(int idx, ref string name, ref string value)
        {
            XmlAttribute attr = this.node.Attributes[idx];
            name = attr.Name;
            value = attr.Value;
        }

        /// <summary>
        /// Gets the attribute by name.
        /// </summary>
        /// <param name="name">The name of the attribute.</param>
        /// <param name="value">The value of the attribute.</param>
        public void GetAttribute(string name, ref string value)
        {
            foreach (XmlAttribute attr in this.node.Attributes)
            {
                if (attr.Name == name)
                {
                    value = attr.Value;
                    return;
                }
            }
        }

        /// <summary>
        /// Gets the child at the specified index.
        /// </summary>
        /// <param name="idx">The position of the child node.</param>
        /// <returns>The <see cref="XmlNodeReader"/> of the child node.</returns>
        public XmlNodeReader ReadChildNode(int idx)
        {
            return new XmlNodeReader((XmlElement)this.node.ChildNodes[idx]);
        }
    }
}
